home *** CD-ROM | disk | FTP | other *** search
/ HamCall (April 1991) / HAMCALL CD-ROM (Buckmaster)(April 1991).BIN / prgming / ctutor / dynlist.c < prev    next >
Text File  |  1990-10-14  |  2KB  |  54 lines

  1.                                          /* Chapter 12 - Program 1 */
  2. main()
  3. {
  4. struct animal {
  5.    char name[25];
  6.    char breed[25];
  7.    int age;
  8. } *pet1, *pet2, *pet3;
  9.  
  10.    pet1 = (struct animal *)malloc(sizeof(struct animal));
  11.    strcpy(pet1->name,"General");
  12.    strcpy(pet1->breed,"Mixed Breed");
  13.    pet1->age = 1;
  14.  
  15.    pet2 = pet1;   /* pet2 now points to the above data structure */
  16.  
  17.    pet1 = (struct animal *)malloc(sizeof(struct animal));
  18.    strcpy(pet1->name,"Frank");
  19.    strcpy(pet1->breed,"Labrador Retriever");
  20.    pet1->age = 3;
  21.  
  22.    pet3 = (struct animal *)malloc(sizeof(struct animal));
  23.    strcpy(pet3->name,"Krystal");
  24.    strcpy(pet3->breed,"German Shepherd");
  25.    pet3->age = 4;
  26.  
  27.        /* now print out the data described above */
  28.  
  29.    printf("%s is a %s, and is %d years old.\n", pet1->name,
  30.            pet1->breed, pet1->age);
  31.  
  32.    printf("%s is a %s, and is %d years old.\n", pet2->name,
  33.            pet2->breed, pet2->age);
  34.  
  35.    printf("%s is a %s, and is %d years old.\n", pet3->name,
  36.            pet3->breed, pet3->age);
  37.  
  38.    pet1 = pet3;   /* pet1 now points to the same structure that
  39.                       pet3 points to                           */
  40.    free(pet3);    /* this frees up one structure               */
  41.    free(pet2);    /* this frees up one more structure          */
  42. /* free(pet1);    this cannot be done, see explanation in text */
  43. }
  44.  
  45.  
  46.  
  47. /* Result of execution
  48.  
  49. Frank is a Laborador Retriever, and is 3 years old.
  50. General is a Mixed Breed, and is 1 years old.
  51. Krystal is a German Shepherd, and is 4 years old.
  52.  
  53. */
  54.